package util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Description: connection pool
 * Created by E440 on 2017/5/17.
 */
public class DataBaseConnectionPool {
    private ComboPooledDataSource dataSource;

    public ComboPooledDataSource getDataSource(){
        return dataSource;
    }

    private static DataBaseConnectionPool pool;
    private DataBaseConnectionPool() throws IOException, PropertyVetoException {
        dataSource = new ComboPooledDataSource();
        Properties prop = new Properties();
        InputStream in = DataBaseConnectionPool.class.getClassLoader().getResourceAsStream("config/c3p0/c3p0.properties");

        prop.load(in);
        String url=prop.getProperty("c3p0.jdbcUrl");
        //System.out.println(url);

        dataSource.setDriverClass(prop.getProperty("c3p0.driverClass"));
        dataSource.setJdbcUrl(prop.getProperty("c3p0.jdbcUrl"));
        dataSource.setUser(prop.getProperty("c3p0.user"));
        dataSource.setPassword(prop.getProperty("c3p0.password"));
    }
    static {
        try {
            pool = new DataBaseConnectionPool();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

      public  final Connection getConnection() throws SQLException {
            return pool.getDataSource().getConnection();
    }

    static public final DataBaseConnectionPool getInstance(){
        return  pool;
    }
}
